package listbyorder.access001_100.test81;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/4 11:27
 */
public class Solution1 {

    // 方法一：注意细节
    public boolean search(int[] nums, int target) {
        if (nums == null || nums.length == 0) return false;
        int l = 0;
        int r = nums.length - 1;
        while (l <= r) {
            int mid = (l + r) >>> 1;
            if (nums[mid] == target) return true;
            if (nums[l] < nums[mid]) {
                // 左边有序
                if (target >= nums[l] && target < nums[mid]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else if (nums[l] == nums[mid]) {
                l++;
            } else {
                // 右边有序
                if (target > nums[mid] && target <= nums[r]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return false;
    }
}
